import os
import json
import random
import re
import math
import datetime
import time
import asyncio
from botpy.message import GroupMessage
from tools import send_message, player_id, player_name, player_number, player_read, player_write, txt_write, txt_exist, bag_add, bag_reduce, bag_enough

async def do_alchemy(message):
    if hasattr(message.author, 'member_openid'):
        number = message.author.member_openid
    elif hasattr(message.author, 'user_openid'):
        number = message.author.user_openid
    uid = await player_id(number)
    play_name = await player_name(uid)
    jobs = await player_read(number, "副职业", None)
    if not any(job["副职业"] == "炼金师" for job in jobs):
        if_job = False
        txt = "\n【你不具备炼金条件！】"
        await send_message(message, 0, 1, txt)
        return
    else:
        if_job = True
        job_level = int([job["等级"] for job in jobs if job["副职业"] == "炼金师"][0])
    use_thing0 = message.content.split("炼金")[1]
    use_thing = use_thing0.split("&")
    if len(use_thing) < 3:
        txt = "\n【炼金格式错误！】\n格式应为：\n[炼金 主材*数量&辅材*数量&反应剂*数量]\n可以留空，例：\n[炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if not use_thing[0]:
        txt = "\n【炼金不可缺少主材！】\n格式：[炼金 主材*数量&辅材*数量&反应剂*数量]\n可以留空，例：[炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if use_thing[1]:
        if_use1 = True
    else:
        if_use1 = False
    if use_thing[2]:
        if_use2 = True
    else:
        if_use2 = False
        
    use_thing_list = []
    for use_thing_item in use_thing:
        if use_thing_item:
            try:
                use_thing_name = use_thing_item.split("*")[0]
                use_thing_number = use_thing_item.split("*")[1]
            except IndexError:
                txt = "\n【格式错误！】\n示例：[炼金 血肉残渣*1&能量余烬*1&天空粉尘*1]"
                await send_message(message, 0, 1, txt)
                return
            use_thing_list.append({"名称": use_thing_name, "数量": int(use_thing_number)})
        else:
            use_thing_list.append({})

    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            if "不够" in await bag_enough(uid, use_thing_list0["名称"], use_thing_list0["数量"]):
                txt = "\n【你缺少相应的炼金材料！】"
                await send_message(message, 0, 1, txt)
                return
    with open(f"data/必备_alchemy.txt", 'r') as f:
        alchemy_data = json.load(f)
    alchemy_result = []
    flag = False
    good_percent_1 = 0
    good_percent_2 = 0
    good_percent_3 = 0

    for alchemy_item in alchemy_data:
        success_percent = 0
        good_percent = 0
        perfect_percent = 0
        for main in alchemy_item["主材"]:
            if main["名称"] == use_thing_list[0]["名称"]:
                good_percent += int(use_thing_list[0]["数量"]) * int(main["成功率"])
                perfect_percent += int(use_thing_list[0]["数量"]) * int(main["大成功率"])
                good_percent *= (1 + 0.02 * job_level)
                perfect_percent *= (1 + 0.01 * job_level)
                if int(use_thing_list[0]["数量"]) >= int(main["数量"]):
                    flag = True
                break
        if good_percent > good_percent_1:
            good_percent_1 = good_percent

        if flag:
            if use_thing_list[1]:
                for main in alchemy_item["辅材"]:
                    if main["名称"] == use_thing_list[1]["名称"]:
                        good_percent += int(use_thing_list[1]["数量"]) * int(main["成功率"])
                        perfect_percent += int(use_thing_list[1]["数量"]) * int(main["大成功率"])
                        break
                if good_percent > good_percent_2:
                    good_percent_2 = good_percent
            else:
                good_percent_2 = good_percent_1


            if use_thing_list[2]:
                for main in alchemy_item["催化剂"]:
                    if main["名称"] == use_thing_list[2]["名称"]:
                        good_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["成功率"])) / 100
                        perfect_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["大成功率"])) / 100
                        break
                if good_percent > good_percent_3:
                    good_percent_3 = good_percent
            else:
                good_percent_3 = good_percent_2

            alchemy_result.append({"名称": alchemy_item["名称"], "成功率": good_percent, "大成功率": perfect_percent, "大成功": alchemy_item["大成功"]})
    result_list = {}
    if len(alchemy_result) > 0:
        weights = [item["成功率"] for item in alchemy_result]
        result = random.choices(alchemy_result, weights=weights)[0]
        result_number = 0
        x = result["成功率"]
        while x > 100:
            y = random.uniform(99, x-1)
            x -= y
            result_number += 1
        if random.uniform(0, 100) <= x:
            result_number += 1
        if result_number > use_thing_list[0]["数量"]:
            result_number = use_thing_list[0]["数量"]
        result_1 = "普通"
        if result_number == 0:
            result_1 = "失败"
            if result["成功率"] > 80:
                if random.random() < 20:
                    get_name = "强烈的气息"
            elif result["成功率"] > 40 and result["成功率"] < 60:
                if random.random() < 10:
                    get_name = "强烈的痕迹"
            else:
                get_name = "焦渣"
            if get_name not in result_list:
                result_list[get_name] = 1
        else:
            for i in range(result_number):
                rand_float = random.uniform(0, 100)
                if rand_float <= result["大成功率"]:
                    get_name = result["大成功"]
                    result["大成功率"] -= rand_float
                    result_1 = "金光"
                    if get_name not in result_list:
                        result_list[get_name] = 1
                    else:
                        result_list[get_name] += 1
                else:
                    get_name = result["名称"]
                    if get_name not in result_list:
                        result_list[get_name] = 1
                    else:
                        result_list[get_name] += 1
    else:
        result = {}
        result["成功率"] = 0
        result["大成功率"] = 0
        result_1 = "失败"

    if good_percent_1 < 20:
        result_txt1 = "\n你将主材渐渐炼化~~~\n一切都是这么的安静。。。\n配方错了？亦或加的太少？"
    elif good_percent_1 < 40:
        result_txt1 = "\n你将主材渐渐炼化~~~\n交融看上去并不活跃~~~\n似乎是加的材料不足吧。。。"
    elif good_percent_1 < 60:
        result_txt1 = "\n你将主材渐渐炼化~~~\n交融正在缓慢地进行~~~\n材料应该是到位了。"
    elif good_percent_1 < 80:
        result_txt1 = "\n你将主材渐渐炼化~~~\n物质的交融还算迅速~~~\n看上去应该没有问题。"
    elif good_percent_1 < 100:
        result_txt1 = "\n你将主材渐渐炼化~~~\n物质的交融一切顺利~~~\n一切尽在掌握之中~~~"
    elif good_percent_1 >= 100:
        result_txt1 = "\n你将主材渐渐炼化~~~\n物质的交融无比顺利，甚至都有能量溢出~~~\n下次少加一点！！！"


    if if_use1:
        result_txt2 = "\n你仔细地添加辅材~~~"
    else:
        result_txt2 = "\n你并没有添加辅材。。。"
    if good_percent_2 < 20:
        result_txt2 += "\n场面安静到能听见呼吸声。。。\n配方貌似不对吧！"
    elif good_percent_2 < 40:
        result_txt2 += "\n物质难以反应，你努力去尝试。。。\n材料是不是加少了？"
    elif good_percent_2 < 60:
        result_txt2 += "\n物质的反应有些艰难。。。\n似乎少了点什么。。。"
    elif good_percent_2 < 80:
        result_txt2 += "\n物质的反应一切正常。。。\n配方应该是对了。。。"
    elif good_percent_2 < 100:
        result_txt2 += "\n物质的反应有条不紊。。。\n太幸运了吧~~~"
    elif good_percent_2 >= 100:
        result_txt2 += "\n物质的反应及其顺利，有不少能量外溢。。。\n材料加多了吧！！！"

    result_txt3 = "\n最后一步，让物质进行反应蜕变~~~"
    if if_use2:
        result_txt3 += "\n你小心翼翼的加入反应剂~~~"
    else:
        result_txt3 += "\n你相信自己的选择，没有添加反应剂，任由物质自行演化。"
    if good_percent_3 < 20:
        result_txt3 += "\n面前的能量剧烈波动，仿佛随时都会崩溃。。。"
    elif good_percent_3 < 40:
        result_txt3 += "\n面前的能量波动激烈，难以控制。。。"
    elif good_percent_3 < 60:
        result_txt3 += "\n面前的光芒忽明忽暗，漂浮不定。。。"
    elif good_percent_3 < 80:
        result_txt3 += "\n面前的光芒十分稳定，内部孕育的气息时隐时现。。。"
    elif good_percent_3 < 100:
        result_txt3 += "\n面前的光芒内，隐约浮现了新事物的轮廓。。。"
    elif good_percent_3 >= 100:
        result_txt3 += "\n面前的光芒里，开始显现新事物的雏形。。。"

    if result_1 == "金光":
        result_txt4 = "\n一阵淡金色的光芒自你面前绽放！！！"
    elif result_1 == "失败":
        result_txt4 = "\n突然间“嘭”的一声巨响！\n浓烟滚滚~~~~~~~~~~"
    elif result_1 == "普通":
        result_txt4 = "\n你感知到了新事物的气息————"
    
    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            await bag_reduce(uid, use_thing_list0["名称"], use_thing_list0["数量"])
    
    await send_message(message, 0, 1, result_txt1)
    await send_message(message, 2, 2, result_txt2)
    await send_message(message, 4, 3, result_txt3)
    await send_message(message, 5, 4, result_txt4)
    await asyncio.sleep(1)

    result_txt5 = "\n【⚗️炼金结果⚗️】"
    if result_list:
        for key,value in result_list.items():
            result_txt5 += "\n◈ [{}] × {}".format(key, value)
            await bag_add(uid, key, value)
    else:
        result_txt5 += "\n【空气】"
    if if_job:
        for job in jobs:
            if job["副职业"] == "炼金师":
                get_exp = int(1)
                if get_exp > job["精力"]:
                    get_exp = job["精力"]
                job["熟练度"] = int(job["熟练度"] + get_exp)
                job["精力"] = int(job["精力"] - get_exp)
                exp_list = [100, 400, 1500, 2500, 4500, 8000, 15000, 40000, 100000, 600000, 9999999999]
                job["熟练度上限"] = exp_list[job["等级"]]
                if job["熟练度"] >= job["熟练度上限"]:
                    job["等级"] += 1
                    job["熟练度"] -= job["熟练度上限"]
                    job["熟练度上限"] = exp_list[job["等级"]]
                    energy_max = [200, 400, 600, 800, 1000, 1500, 2000, 3000, 4000, 5000, 0]
                    job["精力上限"] = energy_max[job["等级"]]
                    result_txt5 += "\n✨【{}】炼金等级提升！✨".format(play_name)
                await player_write(number, "副职业", None, jobs)
    await send_message(message, 0, 5, result_txt5)
    return

async def do_many_alchemy(message):
    if hasattr(message.author, 'member_openid'):
        number = message.author.member_openid
    elif hasattr(message.author, 'user_openid'):
        number = message.author.user_openid
    uid = await player_id(number)
    play_name = await player_name(uid)
    jobs = await player_read(number, "副职业", None)
    if not any(job["副职业"] == "炼金师" for job in jobs):
        if_job = False
        txt = "\n【你不具备炼金条件！】"
        await send_message(message, 0, 1, txt)
        return
    else:
        if_job = True
        job_level = int([job["等级"] for job in jobs if job["副职业"] == "炼金师"][0])
    use_thing0 = message.content.split("批量炼金")[1]
    if len(use_thing0.split("+")) < 2:
        txt = f"\n【批量炼金格式错误！】\n格式应为：\n[批量炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：\n[炼金 血肉残渣*1&&+100]"
        await send_message(message, 0, 1, txt)
        return
    alchemy_number = int(use_thing0.split("+")[1])
    alchemy_thing = use_thing0.split("+")[0]
    use_thing = alchemy_thing.split("&")
    if len(use_thing) < 3:
        txt = "\n【批量炼金格式错误！】\n格式应为：\n[批量炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：\n[炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if not use_thing[0]:
        txt = "\n【批量炼金不可缺少主材！】\n格式：[批量炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：[炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if use_thing[1]:
        if_use1 = True
    else:
        if_use1 = False
    if use_thing[2]:
        if_use2 = True
    else:
        if_use2 = False
        
    use_thing_list = []
    for use_thing_item in use_thing:
        if use_thing_item:
            try:
                use_thing_name = use_thing_item.split("*")[0]
                use_thing_number = use_thing_item.split("*")[1]
            except IndexError:
                txt = "\n【格式错误！】\n示例：[批量炼金 血肉残渣*1&能量余烬*1&天空粉尘*1+3]"
                await send_message(message, 0, 1, txt)
                return
            use_thing_list.append({"名称": use_thing_name, "数量": int(use_thing_number)})
        else:
            use_thing_list.append({})

    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            if "不够" in await bag_enough(uid, use_thing_list0["名称"], use_thing_list0["数量"] * alchemy_number):
                txt = "\n【你缺少相应的炼金材料！】"
                await send_message(message, 0, 1, txt)
                return
    with open(f"data/必备_alchemy.txt", 'r') as f:
        alchemy_data = json.load(f)
    
    alchemy_result = []
    result_list = {}
    for i in range(alchemy_number):
        flag = False
        good_percent_1 = 0
        good_percent_2 = 0
        good_percent_3 = 0

        for alchemy_item in alchemy_data:
            success_percent = 0
            good_percent = 0
            perfect_percent = 0
            for main in alchemy_item["主材"]:
                if main["名称"] == use_thing_list[0]["名称"]:
                    good_percent += int(use_thing_list[0]["数量"]) * int(main["成功率"])
                    perfect_percent += int(use_thing_list[0]["数量"]) * int(main["大成功率"])
                    good_percent *= (1 + 0.02 * job_level)
                    perfect_percent *= (1 + 0.01 * job_level)
                    if int(use_thing_list[0]["数量"]) >= int(main["数量"]):
                        flag = True
                    break
            if good_percent > good_percent_1:
                good_percent_1 = good_percent

            if flag:
                if use_thing_list[1]:
                    for main in alchemy_item["辅材"]:
                        if main["名称"] == use_thing_list[1]["名称"]:
                            good_percent += int(use_thing_list[1]["数量"]) * int(main["成功率"])
                            perfect_percent += int(use_thing_list[1]["数量"]) * int(main["大成功率"])
                            break
                    if good_percent > good_percent_2:
                        good_percent_2 = good_percent
                else:
                    good_percent_2 = good_percent_1


                if use_thing_list[2]:
                    for main in alchemy_item["催化剂"]:
                        if main["名称"] == use_thing_list[2]["名称"]:
                            good_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["成功率"])) / 100
                            perfect_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["大成功率"])) / 100
                            break
                    if good_percent > good_percent_3:
                        good_percent_3 = good_percent
                else:
                    good_percent_3 = good_percent_2

                alchemy_result.append({"名称": alchemy_item["名称"], "成功率": good_percent, "大成功率": perfect_percent, "大成功": alchemy_item["大成功"]})

        if len(alchemy_result) > 0:
            weights = [item["成功率"] for item in alchemy_result]
            result = random.choices(alchemy_result, weights=weights)[0]
            result_number = 0
            x = result["成功率"]
            while x > 100:
                y = random.uniform(99, x-1)
                x -= y
                result_number += 1
            if random.uniform(0, 100) <= x:
                result_number += 1
            if result_number > use_thing_list[0]["数量"]:
                result_number = use_thing_list[0]["数量"]
            result_1 = "普通"
            if result_number == 0:
                result_1 = "失败"
                if result["成功率"] > 80:
                    if random.random() < 20:
                        get_name = "强烈的气息"
                elif result["成功率"] > 40 and result["成功率"] < 60:
                    if random.random() < 10:
                        get_name = "强烈的痕迹"
                else:
                    get_name = "焦渣"
                if get_name not in result_list:
                    result_list[get_name] = 1
                else:
                    result_list[get_name] += 1
            else:
                for i in range(result_number):
                    rand_float = random.uniform(0, 100)
                    if rand_float <= result["大成功率"]:
                        get_name = result["大成功"]
                        result["大成功率"] -= rand_float
                        if get_name not in result_list:
                            result_list[get_name] = 1
                        else:
                            result_list[get_name] += 1
                    else:
                        get_name = result["名称"]
                        if get_name not in result_list:
                            result_list[get_name] = 1
                        else:
                            result_list[get_name] += 1
        else:
            result = {}
            result["成功率"] = 0
            result["大成功率"] = 0
            result_1 = "失败"

    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            use_number = use_thing_list0["数量"] * alchemy_number
            await bag_reduce(uid, use_thing_list0["名称"], use_number)
    

    result_txt5 = "\n【⚗️批量炼金结果⚗️】"
    if result_list:
        for key,value in result_list.items():
            result_txt5 += "\n◈ [{}] × {}".format(key, value)
            await bag_add(uid, key, value)
    else:
        result_txt5 += "\n【空气】"

    if if_job:
        for job in jobs:
            if job["副职业"] == "炼金师":
                get_exp = int(alchemy_number)
                if get_exp > job["精力"]:
                    get_exp = job["精力"]
                job["熟练度"] = int(job["熟练度"] + get_exp)
                job["精力"] = int(job["精力"] - get_exp)
                exp_list = [100, 400, 1500, 2500, 4500, 8000, 15000, 40000, 100000, 600000, 9999999999]
                job["熟练度上限"] = exp_list[job["等级"]]
                if job["熟练度"] >= job["熟练度上限"]:
                    job["等级"] += 1
                    job["熟练度"] -= job["熟练度上限"]
                    job["熟练度上限"] = exp_list[job["等级"]]
                    energy_max = [200, 400, 600, 800, 1000, 1500, 2000, 3000, 4000, 5000, 0]
                    job["精力上限"] = energy_max[job["等级"]]
                    result_txt5 += "\n✨【{}】炼金等级提升！✨".format(play_name)
                await player_write(number, "副职业", None, jobs)
    await send_message(message, 0, 0, result_txt5)
    return


async def help_do_alchemy(message):
    if hasattr(message.author, 'member_openid'):
        number = message.author.member_openid
    elif hasattr(message.author, 'user_openid'):
        number = message.author.user_openid
    uid = await player_id(number)
    play_name = await player_name(uid)
    help_player = (message.content.split("炼金")[0]).split("委托")[1]
    try:
        help_player_uid = int(help_player)
        help_player_number = await player_number(help_player_uid)
    except ValueError:
        txt = "\n【委托uid错误！】".format(resolve_name)
        await send_message(message, 0, 1, txt)
        return
    
    try:
        jobs = await player_read(help_player_number, "副职业", None)
        if not any(job["副职业"] == "炼金师" for job in jobs):
            if_job = False
            txt = "\n【委托者非炼金师！】".format(resolve_name)
            await send_message(message, 0, 1, txt)
            return
        else:
            if_job = True
            job_level = int([job["等级"] for job in jobs if job["副职业"] == "炼金师"][0])
    except TypeError:
        txt = "\n【委托uid错误！】".format(resolve_name)
        await send_message(message, 0, 1, txt)
        return
    need_money = int(10 + (10 + job_level * 1))
    need_txt = await bag_enough(uid, "铜币", need_money)
    if "不够" in need_txt:
        txt = "\n【缺少委托费用[铜币]×{}】".format(need_txt.split("缺")[1])
        await send_message(message, 0, 1, txt)
        return
    use_thing0 = message.content.split("炼金")[1]
    if len(use_thing0.split("+")) < 2:
        txt = f"\n【委托炼金格式错误！】\n格式应为：\n[委托uid炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：\n[委托1炼金 血肉残渣*1&&+100]"
        await send_message(message, 0, 1, txt)
        return
    alchemy_number = int(use_thing0.split("+")[1])
    alchemy_thing = use_thing0.split("+")[0]
    use_thing = alchemy_thing.split("&")
    if len(use_thing) < 3:
        txt = "\n【委托炼金格式错误！】\n格式应为：\n[委托uid炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：\n[委托1炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if not use_thing[0]:
        txt = "\n【委托炼金不可缺少主材！】\n格式：[委托uid炼金 主材*数量&辅材*数量&反应剂*数量+次数]\n可以留空，例：[委托1炼金 血肉残渣*1&&]"
        await send_message(message, 0, 1, txt)
        return
    if use_thing[1]:
        if_use1 = True
    else:
        if_use1 = False
    if use_thing[2]:
        if_use2 = True
    else:
        if_use2 = False
        
    use_thing_list = []
    for use_thing_item in use_thing:
        if use_thing_item:
            try:
                use_thing_name = use_thing_item.split("*")[0]
                use_thing_number = use_thing_item.split("*")[1]
            except IndexError:
                txt = "\n【格式错误！】\n示例：[批量炼金 血肉残渣*1&能量余烬*1&天空粉尘*1+3]"
                await send_message(message, 0, 1, txt)
                return
            use_thing_list.append({"名称": use_thing_name, "数量": int(use_thing_number)})
        else:
            use_thing_list.append({})

    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            if "不够" in await bag_enough(uid, use_thing_list0["名称"], use_thing_list0["数量"] * alchemy_number):
                txt = "\n【你缺少相应的炼金材料！】"
                await send_message(message, 0, 1, txt)
                return
    with open(f"data/必备_alchemy.txt", 'r') as f:
        alchemy_data = json.load(f)
    
    alchemy_result = []
    result_list = {}
    for i in range(alchemy_number):
        flag = False
        good_percent_1 = 0
        good_percent_2 = 0
        good_percent_3 = 0

        for alchemy_item in alchemy_data:
            success_percent = 0
            good_percent = 0
            perfect_percent = 0
            for main in alchemy_item["主材"]:
                if main["名称"] == use_thing_list[0]["名称"]:
                    good_percent += int(use_thing_list[0]["数量"]) * int(main["成功率"])
                    perfect_percent += int(use_thing_list[0]["数量"]) * int(main["大成功率"])
                    good_percent *= (1 + 0.02 * job_level)
                    perfect_percent *= (1 + 0.01 * job_level)
                    if int(use_thing_list[0]["数量"]) >= int(main["数量"]):
                        flag = True
                    break
            if good_percent > good_percent_1:
                good_percent_1 = good_percent

            if flag:
                if use_thing_list[1]:
                    for main in alchemy_item["辅材"]:
                        if main["名称"] == use_thing_list[1]["名称"]:
                            good_percent += int(use_thing_list[1]["数量"]) * int(main["成功率"])
                            perfect_percent += int(use_thing_list[1]["数量"]) * int(main["大成功率"])
                            break
                    if good_percent > good_percent_2:
                        good_percent_2 = good_percent
                else:
                    good_percent_2 = good_percent_1


                if use_thing_list[2]:
                    for main in alchemy_item["催化剂"]:
                        if main["名称"] == use_thing_list[2]["名称"]:
                            good_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["成功率"])) / 100
                            perfect_percent *= (100 + int(use_thing_list[2]["数量"]) * int(main["大成功率"])) / 100
                            break
                    if good_percent > good_percent_3:
                        good_percent_3 = good_percent
                else:
                    good_percent_3 = good_percent_2

                alchemy_result.append({"名称": alchemy_item["名称"], "成功率": good_percent, "大成功率": perfect_percent, "大成功": alchemy_item["大成功"]})

        if len(alchemy_result) > 0:
            weights = [item["成功率"] for item in alchemy_result]
            result = random.choices(alchemy_result, weights=weights)[0]
            result_number = 0
            x = result["成功率"]
            while x > 100:
                y = random.uniform(99, x-1)
                x -= y
                result_number += 1
            if random.uniform(0, 100) <= x:
                result_number += 1
            if result_number > use_thing_list[0]["数量"]:
                result_number = use_thing_list[0]["数量"]
            result_1 = "普通"
            if result_number == 0:
                result_1 = "失败"
                if result["成功率"] > 80:
                    if random.random() < 20:
                        get_name = "强烈的气息"
                elif result["成功率"] > 40 and result["成功率"] < 60:
                    if random.random() < 10:
                        get_name = "强烈的痕迹"
                else:
                    get_name = "焦渣"
                if get_name not in result_list:
                    result_list[get_name] = 1
                else:
                    result_list[get_name] += 1
            else:
                for i in range(result_number):
                    rand_float = random.uniform(0, 100)
                    if rand_float <= result["大成功率"]:
                        get_name = result["大成功"]
                        result["大成功率"] -= rand_float
                        if get_name not in result_list:
                            result_list[get_name] = 1
                        else:
                            result_list[get_name] += 1
                    else:
                        get_name = result["名称"]
                        if get_name not in result_list:
                            result_list[get_name] = 1
                        else:
                            result_list[get_name] += 1
        else:
            result = {}
            result["成功率"] = 0
            result["大成功率"] = 0
            result_1 = "失败"

    for use_thing_list0 in use_thing_list:
        if use_thing_list0:
            use_number = use_thing_list0["数量"] * alchemy_number
            await bag_reduce(uid, use_thing_list0["名称"], use_number)
    await bag_reduce(uid, "铜币", need_money)
    await bag_add(help_player_uid, "铜币", need_money)
    

    result_txt5 = "\n【委托花费[{}]铜币】\n【炼金结果】".format(need_money)
    if result_list:
        for key,value in result_list.items():
            result_txt5 += "\n◈ [{}] × {}".format(key, value)
            await bag_add(uid, key, value)
    else:
        result_txt5 += "\n【空气】"

    if if_job:
        for job in jobs:
            if job["副职业"] == "炼金师":
                get_exp = int(alchemy_number)
                if get_exp > job["精力"]:
                    get_exp = job["精力"]
                job["熟练度"] = int(job["熟练度"] + get_exp)
                job["精力"] = int(job["精力"] - get_exp)
                exp_list = [100, 400, 1500, 2500, 4500, 8000, 15000, 40000, 100000, 600000, 9999999999]
                job["熟练度上限"] = exp_list[job["等级"]]
                if job["熟练度"] >= job["熟练度上限"]:
                    job["等级"] += 1
                    job["熟练度"] -= job["熟练度上限"]
                    job["熟练度上限"] = exp_list[job["等级"]]
                    energy_max = [200, 400, 600, 800, 1000, 1500, 2000, 3000, 4000, 5000, 0]
                    job["精力上限"] = energy_max[job["等级"]]
                    result_txt5 += "\n✨【{}】炼金等级提升！✨".format(play_name)
                await player_write(help_player_number, "副职业", None, jobs)
    await send_message(message, 0, 0, result_txt5)
    return






